
import sun.plugin2.message.GetAppletMessage;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;       //包含JFrame类

public class GameWin extends JFrame{
    MapBottom mapBottom=new MapBottom();
    BottomRay bottomRay=new BottomRay();
    //点击
    MusicPlay musicPlay1=new MusicPlay("imgs/music/1.wav");
    //无效点击
    MusicPlay musicPlay2=new MusicPlay("imgs/music/2.wav");
    //背景
    MusicPlay musicPlay3=new MusicPlay("imgs/music/3.wav");
    //胜利音效
    MusicPlay musicPlay4=new MusicPlay("imgs/music/4.wav");
    Level level=new Level();
    void launch(){
        musicPlay3.musicMain(3);
        this.setVisible(true);      //开启窗口
        this.setSize(GameUtil.width, GameUtil.height);      //大小
        this.setLocationRelativeTo(null);   //居中
        this.setTitle("扫雷");    //标题
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);    //程序结束后关闭窗口
        this.addMouseListener(new MouseAdapter() {      //鼠标事件
            @Override
            public void mousePressed(MouseEvent e) {        //点击事件
                super.mouseClicked(e);
                GameUtil.MOUSE_X=e.getX();
                GameUtil.MOUSE_Y=e.getY();
                if(GameUtil.difficulty==-1) {
                    musicPlay2.musicMain(1);
                    if(e.getButton()==1){
                        if(GameUtil.query((GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2,(GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2+3*GameUtil.SQUARE_LENGTH,
                                4*GameUtil.OFFSET,4*GameUtil.OFFSET+2*GameUtil.SQUARE_LENGTH,GameUtil.MOUSE_X,GameUtil.MOUSE_Y)){
                            GameUtil.difficulty=0;
                        }else if(GameUtil.query((GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2,(GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2+3*GameUtil.SQUARE_LENGTH,
                                7*GameUtil.OFFSET,7*GameUtil.OFFSET+2*GameUtil.SQUARE_LENGTH,GameUtil.MOUSE_X,GameUtil.MOUSE_Y)){
                            GameUtil.difficulty=1;
                        }else if(GameUtil.query((GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2,(GameUtil.OFFSET*2+GameUtil.MAX_W*GameUtil.SQUARE_LENGTH-3*GameUtil.SQUARE_LENGTH)/2+3*GameUtil.SQUARE_LENGTH,
                                10*GameUtil.OFFSET,10*GameUtil.OFFSET+2*GameUtil.SQUARE_LENGTH,GameUtil.MOUSE_X,GameUtil.MOUSE_Y)){
                            GameUtil.difficulty=2;
                        }
                        if(GameUtil.difficulty>=0){
                            bottomRay.init();
                            GameUtil.width=2*GameUtil.OFFSET+GameUtil.SQUARE_LENGTH*GameUtil.MAX_W;
                            GameUtil.height=4*GameUtil.OFFSET+GameUtil.SQUARE_LENGTH*GameUtil.MAX_H;
                        }
                    }
                }else{
                    if(GameUtil.query((GameUtil.OFFSET*2+(GameUtil.MAX_W-1)*GameUtil.SQUARE_LENGTH)/2,(GameUtil.OFFSET*2+(GameUtil.MAX_W-1)*GameUtil.SQUARE_LENGTH)/2+GameUtil.SQUARE_LENGTH-5,
                            GameUtil.OFFSET + 15,GameUtil.OFFSET + GameUtil.SQUARE_LENGTH + 10,GameUtil.MOUSE_X,GameUtil.MOUSE_Y)){
                        bottomRay.init();
                    } else if (GameUtil.state >= 1) {
                        musicPlay1.musicMain(1);
                    } else if (e.getButton() == 1) {
                        musicPlay2.musicMain(1);
                        GameUtil.LEFT = true;
                    } else if(e.getButton()==2){
                        if(GameUtil.query(50,100,50,100,GameUtil.MOUSE_X,GameUtil.MOUSE_Y)){
                            mapBottom.Plug_in();
                        }else {
                            musicPlay1.musicMain(1);
                            GameUtil.difficulty = -1;
                        }
                    }
                    else if (e.getButton() == 3) {
                        musicPlay2.musicMain(1);
                        GameUtil.RIGHT = true;
                    }
                }
            }
        });
        while(true){
            repaint();      //刷新页面
            try {
                Thread.sleep(40);      //每隔多少时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void paint(Graphics g) {     //后绘制，也为主
        Image offScreenImage = this.createImage(GameUtil.width, GameUtil.height);     //创建画布
        Graphics gImage = offScreenImage.getGraphics();     //获取画笔
        gImage.drawImage(GameUtil.background,0,0,GameUtil.width,GameUtil.height,null);
        if(GameUtil.difficulty==-1){
            mapBottom.print_menu(gImage);
        }else {
            this.setSize(GameUtil.width,GameUtil.height);      //大小
            mapBottom.printSelf(gImage);
            if (GameUtil.state == 2) {level.end();}
            else if (level.Victory()) {
                level.victory();
                if(GameUtil.numbei==2) {
                    musicPlay4.musicMain(1);
                    GameUtil.numbei++;
                }
            }
        }
        g.drawImage(offScreenImage, 0, 0, null);       //手动打印，放进去g的话可能出来后会自动打印，放画布的话还需手动打印
    }
    public static void main(String[] args) {
        GameWin gameWin=new GameWin();
        gameWin.launch();
    }
}